import axios from 'axios'
import qs from 'qs'
import { Dialog, Popup } from 'vant'

axios.interceptors.response.use(
  response => {
    // 如果返回的状态码为200，说明接口请求成功，可以正常拿到数据
    if (response.status === 200) {
      return Promise.resolve(response)
    } else {
      return Promise.reject(response)
    }
  },
  error => {
    if (error.response.status) {
      Dialog.alert({
        message: JSON.stringify(error.response.data),
        closeOnClickOverlay: true
      })
      return Promise.reject(error.response)
    }
  }
)

axios.interceptors.request.use(
  async config => {
    return config;
  },
  error => {
    console.log(error.status)
    throw error
  }
)

const Http = {
  get: (url, params) => {
    return axios.get(url, {
        params: params
      })
      .then(res => res.data)
      .catch(err => Promise.reject(err.data))
  },
  post : (url, params) => {
    return axios.post(url, qs.stringify(params))
      .then(res => res.data)
      .catch(err => Promise.reject(err.data))
  }
};

export default Http;


